﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Text;
namespace BYManageInfoSystem
{
    static class Program
    {
        private static Mutex mutex = null;
        private static void GlobalMutex()
        {
            // 是否第一次创建mutex
            bool newMutexCreated = false;
            string mutexName = "Global\\" + "WareHouseMis";//系统名称，Global为全局，表示即使通过通过虚拟桌面连接过来，也只是允许运行一次
            try
            {
                mutex = new Mutex(false, mutexName, out newMutexCreated);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                System.Threading.Thread.Sleep(1000);
                Environment.Exit(1);
            }

            // 第一次创建mutex
            if (newMutexCreated)
            {
                Console.WriteLine("程序已启动");
                //todo:此处为要执行的任务
            }
            else
            {
                MessageBox.Show("另一个窗口已在运行，不能重复运行。");
                System.Threading.Thread.Sleep(1000);
                Environment.Exit(1);//退出程序
            }


        }
        public static string GetHtmlSourceEx(string url, System.Text.Encoding encode, WebProxy proxy)
        {
            try
            {
                WebClient wc = new WebClient();
                if (proxy != null)
                {
                    wc.Proxy = proxy;
                }
                byte[] data = wc.DownloadData(url);
                return encode.GetString(data);
            }
            catch (Exception e)
            {
                return "ok";
            }
        }
        public static bool CheckVer()
        {
            bool bRet = false;
            try
            {
                string strUrl = "http://www.douyh.com/dgc.ashx";
                string src = GetHtmlSourceEx(strUrl, Encoding.Default, null);
                if (src.Trim() == "y")
                {
                    bRet = true;
                }
                return bRet;

            }
            catch (Exception ex)
            {
                return bRet;
            }
        }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //bool bCheck = CheckVer();
            //if (!bCheck)
            //{
            //    MessageBox.Show("请使用最新版本的程序登录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    Environment.Exit(1);
            //    return;
            //}
            GlobalMutex();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(Frm_Main.GetMainForm());
        }
    }
}